package com.example.night.data.InfluencerCoins;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public interface MonthlyInfluencerStatsRepository extends JpaRepository<MonthlyInfluencerStats, Long> {

    // 根据抖音 ID 和月份查询记录
    Optional<MonthlyInfluencerStats> findByPromoteIdAndDate(String promoteId, Date date);

    // 根据抖音 ID 查询所有记录（按月份排序）
    List<MonthlyInfluencerStats> findAllByPromoteIdOrderByDateDesc(String promoteId);

    // 根据月份查询所有记录
    List<MonthlyInfluencerStats> findAllByDate(Date date);

    // 更新订单数和 GMV
    @Transactional
    @Modifying
    @Query("UPDATE MonthlyInfluencerStats m SET m.totalOrders = ?2, m.totalGMV = ?3 WHERE m.promoteId = ?1 AND m.date = ?4")
    int updateOrdersAndGMV(String promoteId, int totalOrders, double totalGMV, Date date);

    // 更新系统服务费和金币数
    @Transactional
    @Modifying
    @Query("UPDATE MonthlyInfluencerStats m SET m.leaderSysServiceMoney = ?2, m.coins = ?3 WHERE m.promoteId = ?1 AND m.date = ?4")
    int updateServiceMoneyAndCoins(String promoteId, double leaderSysServiceMoney, int coins, Date date);

    // 删除某个抖音 ID 下的所有月度统计记录
    @Transactional
    @Modifying
    @Query("DELETE FROM MonthlyInfluencerStats m WHERE m.promoteId = ?1")
    int deleteByPromoteId(String promoteId);
}
